﻿using System;
using System.Runtime.InteropServices;

namespace TimeSyncUtil
{
    public static class SystemTimeHelper
    {

        [DllImport("kernel32.dll")]
        public static extern bool SetSystemTime(ref SystemTime st);

        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime st);

        [DllImport("Kernel32.dll")]
        public static extern void GetSystemTime(ref SystemTime st);

        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime st);

        public static string GetLocalTime()
        {
            SystemTime st = new SystemTime();
            GetLocalTime(ref st);
            return st.ToString();
        }

        public static bool SetLocalTimeByDateTime(DateTime dateTime)
        {
            string dateTimeStr = dateTime.ToNtpTimeStr();
            return SetLocalTimeByStr(dateTimeStr);
        }

        public static string ToNtpTimeStr(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }

        public static bool SetLocalTimeByStr(string timestr)
        {
            bool flag = false;
            SystemTime sysTime = new SystemTime();
            DateTime dt = Convert.ToDateTime(timestr);
            sysTime.wYear = Convert.ToUInt16(dt.Year);
            sysTime.wMonth = Convert.ToUInt16(dt.Month);
            sysTime.wDay = Convert.ToUInt16(dt.Day);
            sysTime.wHour = Convert.ToUInt16(dt.Hour);
            sysTime.wMinute = Convert.ToUInt16(dt.Minute);
            sysTime.wSecond = Convert.ToUInt16(dt.Second);
            try
            {
                flag = SetLocalTime(ref sysTime);
            }
            catch (Exception ex)
            {
                string e = ex.Message;
                return false;
            }
            return flag;
        }

        /// <summary>       
        /// 时间戳转为C#格式时间       
        /// </summary>       
        /// <param name=”timeStamp”></param>       
        /// <returns></returns>       
        public static DateTime ConvertStringToDateTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// 时间戳转为C#格式时间10位
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetDateTimeFrom1970Ticks(long curSeconds)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return dtStart.AddSeconds(curSeconds);
        }

    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;

        public override string ToString()
        {
            return $"{wYear}-{wMonth}-{wDay} {wHour}:{wMinute}:{wSecond}.{wMilliseconds}";
        }
    }
}
